from ast import arg
import time
import queue
import threading 

# Queue.qsize() 返回队列的大小
# Queue.empty() 如果队列为空，返回True,反之False
# Queue.full() 如果队列满了，返回True,反之False
# Queue.full 与 maxsize 大小对应
# Queue.get([block[, timeout]])获取队列，timeout等待时间
# Queue.get_nowait() 相当Queue.get(False)
# Queue.put(item) 写入队列，timeout等待时间
# Queue.put_nowait(item) 相当Queue.put(item, False)
# Queue.task_done() 在完成一项工作之后，Queue.task_done()函数向任务已经完成的队列发送一个信号
# Queue.join() 实际上意味着等到队列为空，再执行别的操作


class MYqueue():
    def __init__(self,queue_size):
        self.name = queue.Queue(queue_size)
    
    def print_queue(self,print_range = 0):
        global waitPrint
        counter = 0
        while waitPrint:
            time.sleep(1)
            if(not self.name.empty()):
              if(print_range == 0):
                queueSize = self.name.qsize()
                for i in range(1,queueSize+1):
                    print(self.name.get())
                    counter = counter + 1
                print("当前已取出共"+ str(counter) + "个数据")
              else:
                for i in range(1,print_range+1):
                    if(self.name.empty()):
                        break
                    else:
                        print(self.name.get())
                        counter = counter + 1
                print("当前已取出共"+ str(counter) + "个数据") 
            else:
               print("当前队列无可读数据")
    def write_queue(self,word,counter = 0,interval = 2):
            i = 0
            if counter == 0 :
                for item in word:
                    self.name.put(item)
                    i = i + 1
                print("当前已放入共"+ str(i) + "个数据")
            else:
                while(i < counter):
                  for item in word:
                     if(i < counter):
                       self.name.put(item)
                       i = i + 1
                     else:
                       break
                  print("当前已放入共"+ str(i) + "个数据")
                  time.sleep(interval)
            
            

class myThread_write(threading.Thread):

    def __init__(self,name,argv,List):
        threading.Thread.__init__(self)
        self.name = name
        self.argv = argv
        self.list = List
    def run(self):
        print(self.name + "开始")
        self.argv.write_queue(self.list,7)
        

class myThread_read(threading.Thread):
    
    def __init__(self,name,argv,Size = 0):
        threading.Thread.__init__(self)
        self.name = name
        self.argv = argv
        self.size = Size
    def run(self):
        print(self.name+ "开始")
        self.argv.print_queue(self.size)
        

def do_test():
    global waitPrint
    threadList = ["Thread-1", "Thread-2", "Thread-3"]
    nameList = ["One", "Two", "Three", "Four", "Five"]        
    NumQueue = MYqueue(20)
    
    # NumQueue.write_queue(nameList)
    # NumQueue.print_queue(3)
    # NumQueue.write_queue(threadList)
    # NumQueue.print_queue(1)
    # NumQueue.print_queue()

    thread_write1 = myThread_write("线程1写入",NumQueue,nameList)
    thread_read2 = myThread_read("线程2读取",NumQueue,1)

    thread_write1.start()
    thread_read2.start()
    
    while not NumQueue.name.empty():
       pass
    
    waitPrint = 0
    print("读取结束")
    thread_write1.join()
    thread_read2.join()


    print("主线程退出")

if __name__ == '__main__':
    global waitPrint 
    waitPrint = 1
    do_test()


